<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Layout {

    public $obj;
    //public $_menu;
    public $layout;
    public $_admin_controller_menu = array();

    function Layout($layout = "html_layout") {
        $this->obj = & get_instance();
        
   //     $this->obj->load->library('session');

        

        $this->layout = $layout;
    }
    function getAdminMenu(){
        $menuAdmin = $this->obj->config->item('admin_controller_menu');
    }
    function setLayout($layout) {
        $this->layout = $layout;
    }
    function view($view, $data=null, $return=false) {
	$views = array();
        $userData = array();
        $logged = false;
        if ($this->obj->session && $this->obj->session->userdata('logged_in')){
            $userData['nombre']  =  $this->obj->session->userdata('nombre');
            $userData['uploadfolder']  =  $this->obj->session->userdata('uploadfolder');
            $views = $this->obj->session->userdata('resources');
            $userData['avatar']  =  "";
            $logged = true;
        }
        $loadedData = array();
        $headerView = '/include/sections/header/header';
        $footerView = '/include/sections/footer/footer';
        
        
        $roles = array();
        
        $layout_header['views'] = $views;
        $layout_header['rol'] = $roles;
        $layout_header['logged'] = $logged;
        $layout_header['user'] = $userData;
        $layout_header['admin_controller'] = $this->obj->config->item('admin_controller_menu');
        
        $layout_footer['views'] = $views;
        $layout_footer['rol'] = $roles;
        $layout_footer['logged'] = $logged;
        $layout_footer['admin_controller'] = $this->obj->config->item('admin_controller_menu');
        
        $loadedData['header'] = $this->obj->load->view($headerView,$layout_header,true);
        $loadedData['footer'] = $this->obj->load->view($footerView,$layout_footer,true);


//	$this->benchmark->mark('code_end');

//        $loadedData['response_time'] = $this->benchmark->elapsed_time('code_start', 'code_end');
        
        $loadedData['content_for_layout'] = $this->obj->load->view($view, $data, true);
        if ($return) {
            $output = $this->obj->load->view($this->layout, $loadedData, true);
            return $output;
        } else {
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }
    /*
     * imprime layout segun tipo 'ajax' o 'json'
     */
    function printLike($data,$type='ajax') {
        $this->layout = $type.'_layout';
        if($type !== 'html'){
            $loadedData['content_for_layout'] = $data;
            $this->obj->load->view($this->layout, $loadedData, false);
        }
    }
}

?>
